/*
 * Copyright (C) 2004-2010  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "stdafx.h"
#include "FdoCMSXmlGlobals.h"
#include "FdoCMSBingGetMap.h"
#include <FdoCommonStringUtil.h>
#include <OWS/FdoOwsGlobals.h>

FdoCMSBingGetMap::FdoCMSBingGetMap ():
        m_dCenterLat(0.0),
        m_dCenterLon(0.0),
        m_iZoomLevel(0),
        m_Height(0),
        m_Width(0)
{
}

FdoCMSBingGetMap::FdoCMSBingGetMap (FdoDouble dCenterLat,
                                    FdoDouble dCenterLon,
                                    FdoInt32 iZoomLevel,
                                    FdoSize height,
                                    FdoSize width,
                                    FdoString* imageFormat,
                                    FdoString* mapType) 
    : m_dCenterLat(dCenterLat), 
      m_dCenterLon(dCenterLon),
      m_iZoomLevel(iZoomLevel),
      m_Height(height),
      m_Width(width), 
      m_Format(imageFormat ? imageFormat : L""), 
      m_MapType(mapType ? mapType : L"")
{
}

FdoCMSBingGetMap* FdoCMSBingGetMap::Create (FdoDouble dCenterLat, 
                                            FdoDouble dCenterLon,
                                            FdoInt32 iZoomLevel,
                                            FdoSize height,
                                            FdoSize width,
                                            FdoString* imageFormat,
                                            FdoString* mapType) 
{
    return new FdoCMSBingGetMap (dCenterLat, dCenterLon, iZoomLevel, height, width, imageFormat, mapType);
}

FdoCMSBingGetMap::~FdoCMSBingGetMap ()
{
}

FdoStringP FdoCMSBingGetMap::EncodeKVP()
{
    // For common request, version and service

    // Add "c" parameters in the request    
    FdoStringP ret = FdoCMSXmlGlobals::CMSBingRequestCenter;
    ret += FdoOwsGlobals::Equal;
    ret += FdoStringP::Format(L"%lf", m_dCenterLat);
    ret += FdoCMSXmlGlobals::CMSRequestComma;
    ret += FdoStringP::Format(L"%lf", m_dCenterLon);

    // Add "w" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSBingRequestWidth;
    ret += FdoOwsGlobals::Equal;
    ret += FdoStringP::Format(L"%d", m_Width);

    // Add "h" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSBingRequestHeight;
    ret += FdoOwsGlobals::Equal;
    ret += FdoStringP::Format(L"%d", m_Height);

    // Add "o" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSBingRequestFormat;
    ret += FdoOwsGlobals::Equal;
    ret += m_Format;

    // Add "b" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSBingRequestMapType;
    ret += FdoOwsGlobals::Equal;
    ret += m_MapType;

    // Add "z" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoCMSXmlGlobals::CMSBingRequestZoom;
    ret += FdoOwsGlobals::Equal;
    ret += FdoStringP::Format(L"%d", m_iZoomLevel);
    
    // Translate to a valid HTTP request
    ret = ret.Replace(L" ", L"%20"); 
    //ret = L"c=43.621,-102.349&w=200&h=200&o=gif&b=h&z=16"; 
    return ret;
}

FdoStringP FdoCMSBingGetMap::EncodeXml()
{
    // TODO: shall we support this?
    return L"";
}
